package com.ease.util.collections {
	import flash.events.EventDispatcher;

	import mx.collections.ArrayCollection;


	/**
	 *
	 * 这个MapProvider里面的键值对是一一对应的，键值都是唯一的。
	 *
	 *
	 */
	public class MapProvider extends EventDispatcher implements Map {

		private var _list:ArrayCollection;

		public function MapProvider() {
			_list = new ArrayCollection()
		}

		public function values():Array{
			var array:ArrayCollection = new ArrayCollection();
			for each(var entry:MapEntry in _list){
				array.addItem(entry.value);
			}
			return array.source;
		}

		public function keys():Array {
			var array:ArrayCollection = new ArrayCollection();
			for each(var entry:MapEntry in _list){
				array.addItem(entry.key);
			}
			return array.source;
		}

		public function getObject(key:Object):Object {
			for each(var entry:MapEntry in _list){
				if(entry.key == key){
					return entry.value;
				}
			}
			return null;
		}

		public function getKey(value:Object):Object {
			for each(var entry:MapEntry in _list){
				if(entry.value == value){
					return entry.key;
				}
			}
			return null;
		}

		public function containsKey(key:Object):Boolean {
			return this.indexKey(key) != -1;
        }

       	public function containsValue(value:Object):Boolean {
			return this.indexValue(value) != -1;
        }


		public function put(key:Object,value:Object):Object {
			var entry:MapEntry = new MapEntry();
			entry.key = key;
			entry.value = value;
			return addMapEntry(entry);
		}

		private function addMapEntry(entry:MapEntry):Object {
			if(this._list.getItemIndex(entry) == -1){
				var indexKey:int = this.indexKey(entry.key);
				var indexValue:int = this.indexValue(entry.value);
				if(indexKey != -1){
					var oldEntry:MapEntry = this._list.getItemAt(indexKey) as MapEntry;
					oldEntry.value = entry.value;
					return oldEntry.value;
				}
//				else if(indexValue != -1){
//					this._list.addItemAt(entry,indexValue);
//				}
				else
					this._list.addItem(entry);
			}
			return null;
		}

		/**
		 * 这个list当中必须为MapEntry对象
		 *
		 */
		public function putAllList(list:ArrayCollection):void {
			for each(var value:Object in list){
				if(value is MapEntry){
					var entry:MapEntry = value as MapEntry;
					addMapEntry(entry);
				}
			}
		}

		/**
		 * 这个list当中必须为MapEntry对象
		 *
		 *
		 */
		public function set list(list:ArrayCollection):void {
			this._list = list;
		}

		public function putAll(map:Map):void {
			var keys:Object = map.keys();
			for each(var key:Object in keys){
				var value:Object =map.getObject(key);
				this.put(key,value);
			}
		}

		public function remove(key:Object):Object {
			var index:int = this.indexKey(key);
			if(index>=0){
				var entry:MapEntry = this._list.removeItemAt(index) as MapEntry;
				if(entry != null){
					return entry.value;
				}
			}
			return null;
		}

		public function removeByValue(value:*):void {
			var index:int = this.indexValue(value);
			if(index>=0){
				this._list.removeItemAt(index);
			}
		}

		public function clear():void {
			_list.removeAll();
		}

		public function isEmpty():Boolean {
			return this._list.length > 0;
		}

		public function size():uint{
			return this._list.length;
		}

		private function indexKey(key:Object):int {
			for each(var entry:MapEntry in _list){
				if(entry.key == key){
					return this._list.getItemIndex(entry);
				}
			}
			return -1;
		}

		private function indexValue(value:Object):int {
			for each(var entry:MapEntry in _list){
				if(entry.value == value){
					return this._list.getItemIndex(entry);
				}
			}
			return -1;
		}
	}
}